//
//  GamePlayLayer.cpp
//  popstar
//
//  Created by luyuhuang on 14/11/28.
//
//

#include "GamePlayLayer.h"
#include "GamePlayManager.h"

bool GamePlayLayer::init()
{
    
    auto s = Director::getInstance()->getWinSize();
    // Add Background
    
    auto background = Sprite::create("bg_mainscene.jpg");
    background->setAnchorPoint(Vec2(0.5, 0.5));
    background->setPosition(s.width/2, s.height/2);
    
    this->addChild(background);
    
    
    m_manager = GamePlayManager::create(this);
    
    m_manager->retain();
    
    // 创建一个事件监听器类型为 OneByOne 的单点触摸
    auto listener1 = EventListenerTouchOneByOne::create();
    // 设置是否吞没事件，在 onTouchBegan 方法返回 true 时吞没
    listener1->setSwallowTouches(true);
    
    // 使用 lambda 实现 onTouchBegan 事件回调函数
    listener1->onTouchBegan = [=](Touch* touch, Event* event){
        Point p = touch->getLocationInView();
        p = Director::getInstance()->convertToGL(p);
        CCLOG("x=%f y=%f",p.x,p.y);
        if(m_manager){
            m_manager->onTouch(p);
        }
        return true;
    };
    
    
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, this);
    
    scheduleUpdate();
    
    return true;
}



void GamePlayLayer::update(float dt)
{
    
    
  //  CCLOG("Update!!!!");
    
    if (m_manager) {
        m_manager->updateMatrx();
        
    }
    
}

GamePlayLayer::~GamePlayLayer()
{
    
    m_manager->release();
    
}
